local sk__kuangfeng = fk.CreateSkill {

  name = "sk__kuangfeng",

  tags = {},

}



sk__kuangfeng:addEffect(fk.EventPhaseStart, {
  name = "sk__kuangfeng",
  events = { fk.EventPhaseStart, fk.DamageInflicted },
  anim_type = "offensive",
  expand_pile = "sk__godzhugeliang_star",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__kuangfeng.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Start and #player:getPile("sk__godzhugeliang_star") > 0
      else
        return target:getMark("@sk__kuangfeng") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local success, dat = player.room:askForUseActiveSkill(player, "sk__kuangfeng_active", "#sk__kuangfeng-choose", true,
        nil, false)
      if success then
        event:setCostData(self, dat)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:moveCardTo(event:getCostData(self).cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile,
        sk__kuangfeng.name, nil, true, player)
      local to = room:getPlayerById(event:getCostData(self).targets[1])
      if not to.dead then
        room:addPlayerMark(to, "@sk__kuangfeng", 1)
      end
    else
      if data.damageType == fk.NormalDamage then
        player:drawCards(target:getMark("@sk__kuangfeng"), sk__kuangfeng.name)
      elseif data.damageType == fk.FireDamage then
        data.damage = data.damage + target:getMark("@sk__kuangfeng")
      elseif data.damageType == fk.ThunderDamage then
        player:addToPile("sk__godzhugeliang_star", room:getNCards(1), false, sk__kuangfeng.name, player.id)
      end
    end
  end,
})
sk__kuangfeng:addEffect(fk.DamageInflicted, {
  name = "sk__kuangfeng",
  events = { fk.EventPhaseStart, fk.DamageInflicted },
  anim_type = "offensive",
  expand_pile = "sk__godzhugeliang_star",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__kuangfeng.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Start and #player:getPile("sk__godzhugeliang_star") > 0
      else
        return target:getMark("@sk__kuangfeng") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local success, dat = player.room:askForUseActiveSkill(player, "sk__kuangfeng_active", "#sk__kuangfeng-choose", true,
        nil, false)
      if success then
        event:setCostData(self, dat)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.damageType == fk.NormalDamage then
      player:drawCards(target:getMark("@sk__kuangfeng"), sk__kuangfeng.name)
    elseif data.damageType == fk.FireDamage then
      data.damage = data.damage + target:getMark("@sk__kuangfeng")
    elseif data.damageType == fk.ThunderDamage then
      player:addToPile("sk__godzhugeliang_star", room:getNCards(1), false, sk__kuangfeng.name, player.id)
    end
  end,
})
sk__kuangfeng:addEffect(fk.EventLoseSkill, {
  refresh_events = { fk.EventLoseSkill, fk.Deathed },
  can_refresh = function(self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
          return p:hasSkill("sk__kuangfeng", true, true)
        end) then
      if event == fk.EventLoseSkill then
        return data.skill.name == sk__kuangfeng.name
      else
        return player:hasSkill("sk__kuangfeng", true, true)
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__kuangfeng", 0)
    end
  end,
})
sk__kuangfeng:addEffect(fk.Deathed, {
  refresh_events = { fk.EventLoseSkill, fk.Deathed },
  can_refresh = function(self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
          return p:hasSkill("sk__kuangfeng", true, true)
        end) then
       return player:hasSkill("sk__kuangfeng", true, true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__kuangfeng", 0)
    end
  end,
})

return sk__kuangfeng
